<template>
    <h2>借款显示页面</h2>
    借款款项名称: <input type="text" v-model="BName" />
    利息编号: <select v-model="Iid">
        <option v-for="i in data" :key="i.Iid" :value="i.Iid">{{ i.IYear }}年，利息为：{{ i.Ilixi }}</option>
    </select>
    <button @click="show">查询</button>
    <table>
        <thead>
            <tr>
                <th>借款款项名称</th>
                <th>借款最高金额</th>
                <th>借款最高年限</th>
                <th>月还款</th>
                <th>利息编号</th>
                <th>借款款项简介</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="i in datas" :key="i.BId" :value="i.BId">
                <td>{{ i.BName }}</td>
                <td>{{ i.BPrice }}</td>
                <td>{{ i.BYear }}</td>
                <td>{{ (i.BPrice / i.BYear / 12).toFixed(2) }}NPY</td>
                <td>{{ i.Interests.IYear }}年，利息为：{{ i.Interests.Ilixi }}</td>
                <td>{{ i.BInfo }}</td>
                <td>
                    <input type="button" value="删除" @click="shan(i.BId)" />
                    <input type="button" value="修改" @click="fan(i.BId)" />
                </td>
            </tr>
        </tbody>
    </table>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
const router = useRouter()
const datas: any = ref([])
const BName = ref("")
const Iid = ref("")
const data: any = ref([])
onMounted(() => {
    bind();
    show();
})
const bind = () => {
    axios.get("http://localhost:55817/api/Tian/IntestBind")
        .then(res => {
            data.value = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}
const show = () => {
    axios.get("http://localhost:55817/api/Tian/ShowBorrow", {
        params: {
            name: BName.value,
            iid: Iid.value
        }
    })
        .then(res => {
            datas.value = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}
const shan = (BId: any) => {
    if (confirm("确定要删除吗？")) {
        axios.get("http://localhost:55817/api/Tian/DeleteBorrow", {
            params: {
                id: BId
            }
        })
            .then(res => {
                if (res.data > 0) {
                    alert("删除成功");
                    show();
                }
                else {
                    alert("删除失败");
                    return;
                }
            })
            .catch(err => {
                console.log(err);
            })
    }
}
//反填
const fan = (BId: any) => {
    router.push({
        name: 'UpdBorrow',
        params: {
            BId: BId
        }
    })
}
</script>